import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;


public class GUI extends javax.swing.JFrame {
	private JPanel panelGeneral;
	private JPanel panelTablero;
	private JPanel panelBotones;
	private JPanel panelPlantas;
	private JButton botonStart;
	private JButton botonNuevo;
	private JLabel labelPlantas;
	private JPanel panelStart;
	
	
	private Partida juego;
	private JLabel fondoTablero;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GUI inst = new GUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public GUI() {
		super();
		initGUI();
		initvariablesprogramador();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setResizable(false);
			this.setTitle("Plantas vs. Zombies");
			{
				panelGeneral = new JPanel();
				BorderLayout panelGeneralLayout = new BorderLayout();
				getContentPane().add(panelGeneral, BorderLayout.CENTER);
				panelGeneral.setLayout(panelGeneralLayout);
				{
					panelTablero = new JPanel();
					panelTablero.setLayout(null);
					panelGeneral.add(panelTablero, BorderLayout.CENTER);
					panelTablero.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				}
				{
					panelBotones = new JPanel();
					BorderLayout panelBotonesLayout = new BorderLayout();
					panelBotones.setLayout(panelBotonesLayout);
					panelGeneral.add(panelBotones, BorderLayout.NORTH);
					panelBotones.setPreferredSize(new java.awt.Dimension(10, 100));
					{
						panelPlantas = new JPanel();
						GridBagLayout panelPlantasLayout = new GridBagLayout();
						panelPlantasLayout.rowWeights = new double[] {0.1};
						panelPlantasLayout.rowHeights = new int[] {7};
						panelPlantasLayout.columnWeights = new double[] {0.1};
						panelPlantasLayout.columnWidths = new int[] {7};
						panelPlantas.setLayout(panelPlantasLayout);
						panelBotones.add(panelPlantas, BorderLayout.CENTER);
						panelPlantas.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
						{
							labelPlantas = new JLabel();
							FlowLayout labelPlantasLayout = new FlowLayout();
							panelPlantas.add(labelPlantas, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							labelPlantas.setLayout(labelPlantasLayout);
							labelPlantas.setText("SECCION PARA LAS PLANTAS");
						}
					}
					{
						panelStart = new JPanel();
						GridBagLayout panelStartLayout = new GridBagLayout();
						panelStartLayout.rowWeights = new double[] {0.1};
						panelStartLayout.rowHeights = new int[] {7};
						panelStartLayout.columnWeights = new double[] {0.1};
						panelStartLayout.columnWidths = new int[] {7};
						panelStart.setLayout(panelStartLayout);
						panelBotones.add(panelStart, BorderLayout.EAST);
						panelStart.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
						{
							botonNuevo = new JButton();
							panelStart.add(botonNuevo); //, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							botonNuevo.setText("Nueva Partida");
							botonNuevo.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									botonNuevoActionPerformed(evt);
								}
							});
							
							botonStart = new JButton();
							panelStart.add(botonStart); //, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							botonStart.setText("Empezar");
							botonStart.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									botonStartActionPerformed(evt);
								}
							});
						}
					}
				}
			}
			pack();
			this.setSize(458, 604);
		} catch (Exception e) {
		    
			e.printStackTrace();
		}
	}
	
	private void initvariablesprogramador() {
			Vector<Zombie> zombies = new Vector<Zombie>(10);
			juego = new Partida(zombies);
		
			for (int i=0;i<10; i++) {
				Random r = new Random();
				int fila = r.nextInt(6); // se genera una fila aleatoria para el zombie entre 0 y 5
				JLabel lzombie = new JLabel();
				lzombie.setIcon(new ImageIcon(getClass().getClassLoader().getResource("zombie2.gif")));
				lzombie.setBounds(350, fila*80-20,100,100);
				Zombie z = new Zombie(lzombie);
				agregarZombie(z);
				juego.agregarZombie(z);

			}
			
			fondoTablero = new JLabel();
			panelTablero.add(fondoTablero);
			fondoTablero.setBounds(-1, -3, 450, 480);
			fondoTablero.setOpaque(true);
			fondoTablero.setIcon(new ImageIcon(getClass().getClassLoader().getResource("tablero.jpg")));
	}
			
	public void agregarZombie(Zombie z) {
		panelTablero.add(z.getSprite());
	}
	
	private void botonStartActionPerformed(ActionEvent evt) {
		botonStart.setEnabled(false);
		for(Zombie zombie: juego.getZombies()) {
			HiloZombie hz = new HiloZombie(350, 0, zombie.getSprite(), this);
			juego.hilos.add(hz);
		}
		juego.comenzarPartida();
			
	}
	
	private void botonNuevoActionPerformed(ActionEvent evt) {
		botonStart.setEnabled(true);
		juego.zombies.removeAllElements();
		juego.detenerPartida();
		panelTablero.removeAll();
		this.initvariablesprogramador();
	}
	
	public void perdio() {
		juego.zombies.removeAllElements();
		panelTablero.removeAll();
		JOptionPane.showMessageDialog(this, "Usted ha perdido. GAME OVER !");
	}

}
